package com.thinkgsp.webapp.controller.rp;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.thinkgsp.jxc.model.Emp;
import com.thinkgsp.jxc.model.RpRecBill;
import com.thinkgsp.jxc.service.RpBillCheckService;
import com.thinkgsp.jxc.service.RpBillService;
import com.thinkgsp.jxc.service.ServiceException;
import com.thinkgsp.sys.model.SysAccountPeriod;
import com.thinkgsp.sys.service.NumberRoleService;
import com.thinkgsp.webapp.util.ExtReturn;

/**
 * 收款单控制器
 * @author evan
 *
 */
@Controller
@RequestMapping(value="/rp/recbill")
public class RecBillController {

	@Autowired
	private RpBillService rpBillService;
	@Autowired
	private NumberRoleService numberRoleService;
	@Autowired
	private RpBillCheckService billCheckService;
	
	@RequestMapping(value="/query.do")
	@ResponseBody
	public Object queryRecBills(@RequestParam("begDate") Date begDate,@RequestParam("endDate")Date endDate,
			@RequestParam("custId")Integer custId,@RequestParam("start") int start,@RequestParam("limit") int limit) {
		if(custId == null || custId == 0) {
			return rpBillService.getRecBillList(begDate, endDate,start,limit);
		} else {
			return rpBillService.getRecBillList(begDate, endDate, custId,start,limit);
		}
	}
	
	@RequestMapping(value="/createRecBill.do",method=RequestMethod.GET)
	@ResponseBody
	public Object createRecBill() {
		RpRecBill bill = new RpRecBill();
		SysAccountPeriod period = rpBillService.getCurrentPeriod();
		bill.setFyear(period.getFyear());
		bill.setPeriod(period.getPeriod());
		Emp emp = rpBillService.getCurrentEmp();
		bill.setEmp(emp);
		bill.setMaker(emp);
		bill.setDept(rpBillService.getCurrentUser().getLogonDept());
		try {
			String code = numberRoleService.newNumber(9);
			bill.setRpBillCode(code);
		} catch(ServiceException e) {
			Log.error("New RecBill codeNumber error:" + e.getMessage());
		}
		List<RpRecBill> list = new ArrayList<RpRecBill>();
		list.add(bill);
		return list;
	}
	
	@RequestMapping(value="/getRecBill.do")
	@ResponseBody
	public Object getRecBill(@RequestParam Long billId) {
		try {
			RpRecBill bill = rpBillService.getRecBill(billId);
			List<RpRecBill> bills = new ArrayList<RpRecBill>();
			bills.add(bill);
			return bills;
		} catch(Exception e) {
			return new ExtReturn("获取收款单出错.");
		}
	}
	
	@RequestMapping(value="/save.do",method=RequestMethod.POST)
	@ResponseBody
	public Object saveRecBill(@RequestBody RpRecBill bill) {
		if(bill == null) {
			return new ExtReturn("单据无效。");
		}
		try {
			rpBillService.saveRecBill(bill);
			return new ExtReturn(true,"单据保存成功!",bill);
		} catch(Exception e) {
			return new ExtReturn("保存收款单失败，原因:" + e.getMessage());
		}
	}
	@RequestMapping(value="/archive.do")
	@ResponseBody
	public Object archiveRecBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			rpBillService.archiveRecBill(billId);
			return new ExtReturn(true,"收款单归档成功!");
		} catch(Exception e) {
			return new ExtReturn(false,"单据归档失败,原因:" + e.getMessage());
		}
	}
	@RequestMapping(value="/unArchive.do")
	@ResponseBody
	public Object unArchiveRecBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			rpBillService.unArchiveRecBill(billId);
			return new ExtReturn(true,"收款单反归档成功!");
		} catch(Exception e) {
			return new ExtReturn(false,"单据反归档失败,原因:" + e.getMessage());
		}
	}
	@RequestMapping(value="/autoCheck.do")
	@ResponseBody
	public Object autoCheckBill(@RequestParam Long billId) {
		try {
			billCheckService.autoCheckRecBill(billId);
			return new ExtReturn(true,"核销成功!");
		} catch(Exception e) {
			Log.error(e);
			return new ExtReturn(false,"核销失败,原因:" + e.getMessage());
		}
	}
	@RequestMapping(value="/delete.do")
	@ResponseBody
	public Object deleteRecBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			rpBillService.deleteRecBill(billId);
			return new ExtReturn(true,"收款单删除成功!");
		} catch(Exception e) {
			return new ExtReturn(false,"单据删除失败,原因:" + e.getMessage());
		}
	}
	
	@RequestMapping(value="/getUnSettleARecBill.do")
	@ResponseBody
	public Object getUnSettleARecBill(@RequestParam Integer custId) {
		return rpBillService.getUnSettleARecBill(custId);
	}
}
